const { Approval } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { approvalID, workOrderID, approverUserID } = req.body
    try {
        switch (operation) {
            case 'create':
                const data = await Approval.create({
                    WorkOrderID: workOrderID,//工单ID
                    ApproverUserID: approverUserID,//审批人ID
                    ApprovalDate: new Date(),//审批日期
                    ApprovalStatus: 0 //审批状态
                })
                res.status(200).send({
                    data,
                    meta: {
                        msg: "创建成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'read':
                const approval = await Approval.findAll()
                console.log(approval);
                res.status(200).send({
                    data: approval,
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'delete':
                const delData = await Approval.destroy({ where: { approvalID: approvalID } })
                res.status(200).send({
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            default:
                break;
        }

    } catch (error) {
        res.status(500).send({ message: 'Internal server error', error: error.message });
    }
}